package com.zhuzi;

import org.apache.commons.cli.*;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;

/**
 * @Author: MI
 * @Date: 2021/10/23/21:27
 * @Description:
 */
public class ListSpringJar {
    public static void main(String[] args) {
        // java解析命令行参数|https://blog.csdn.net/weixin_39616603/article/details/100121255
        // 根据命令行参数定义Option对象，第1/2/3/4个参数分别是指命令行参数名缩写、参数名全称、是否有参数值、参数描述
        Option opt1 = new Option("d", "dir", true, "spring 源码位置(eg:D:/soft/apache-maven-3.6.0/repo/org/springframework)");
        opt1.setRequired(false);
        Options options = new Options();
        options.addOption(opt1);

        CommandLine cli = null;
        CommandLineParser cliParser = new DefaultParser();
        HelpFormatter helpFormatter = new HelpFormatter();

        try {
            cli = cliParser.parse(options, args);
        } catch (ParseException e) {
            // 解析失败是用 HelpFormatter 打印 帮助信息
            helpFormatter.printHelp(">>>>>>   cli options", options);
            e.printStackTrace();
        }
        File file = null;
        if (cli.hasOption("d")) {
            String optionValue = cli.getOptionValue("d");
            System.out.println(optionValue);
            file = new File(optionValue);
            final Collection<File> files = FileUtils.listFiles(file, new String[]{"jar"}, true);
            files.stream().parallel().forEach(item -> {
                if (item.getName().endsWith("sources.jar")) {
                    System.out.println("deal with :" + item.getName());
                    unzip(item.getAbsolutePath(), item.getParentFile().getAbsolutePath() + "/sources");
                    item.delete();
                }
                if (item.getName().endsWith("sources.jar.sha1")) {
                    item.delete();
                }
            });
        }
    }

    /**
     * 解压Zip文件
     *
     * @param zipFile 需要解压的zip文件位置
     * @param destDir 解压的目标位置
     */
    public static void unzip(String zipFile, String destDir) {
        File f;
        try (ArchiveInputStream i = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, Files.newInputStream(Paths.get(zipFile)))) {
            ArchiveEntry entry;
            while ((entry = i.getNextEntry()) != null) {
                if (!i.canReadEntryData(entry)) {
                    continue;
                }
                f = new File(destDir, entry.getName());
                if (entry.isDirectory()) {
                    if (!f.isDirectory() && !f.mkdirs()) {
                        throw new IOException("failed to create directory " + f);
                    }
                } else {
                    File parent = f.getParentFile();
                    if (!parent.isDirectory() && !parent.mkdirs()) {
                        throw new IOException("failed to create directory " + parent);
                    }
                    try (OutputStream o = Files.newOutputStream(f.toPath())) {
                        IOUtils.copy(i, o);
                    }
                }
            }
        } catch (IOException | ArchiveException e) {
            e.printStackTrace();
        }

    }
}
